#!/usr/bin/sh
#Version 190127.1

echo "Topfield TF77X0HDPVR Enigma2"

rm -f /dev/vfd
rm -f /dev/rc
rm -f /dev/fpc
mknod -m 0666 /dev/fpc     c 62 0 2>/dev/null
mknod -m 0666 /dev/vfd     c 62 0 2>/dev/null
mknod -m 0666 /dev/rc      c 62 1 2>/dev/null
mknod -m 0666 /dev/fplarge c 62 2 2>/dev/null
mknod -m 0666 /dev/fpsmall c 62 3 2>/dev/null

echo "Init frontpanel"
modprobe e2_proc
modprobe tffp

modprobe simu_button
tffpctl --keyemulationmode 2

if [ -e /var/etc/.firstboot ]; then
   echo "WAIT" > /dev/fpsmall
   echo "3 minutes" > /dev/fplarge
   rm /var/etc/.firstboot
   touch /dev/.devfsd
else
   echo "LOAD" > /dev/fplarge
   echo "  10" > /dev/fpsmall
   echo "[rcS] LOAD 10"
fi

# remove /dev/ttyAS1 to avoid interference with the FP driver
if [ -e /dev/ttyAS1 ]; then
   rm -f /dev/ttyAS1
fi

# Update the GMT offset in case it changed during the standby
/usr/bin/tffpctl --setgmtoffset

if [ -e /etc/init.d/mountvirtfs ]; then
   /etc/init.d/mountvirtfs start
fi

if [ -e /etc/init.d/portmap ]; then
   /etc/init.d/portmap start
fi

if [ -e /etc/init.d/autofs ]; then
   /etc/init.d/autofs start
fi

echo "Loading modules"

echo "LOAD" > /dev/fplarge
echo "   9" > /dev/fpsmall
echo "[rcS] Init stmfb (LOAD 9)"
modprobe stmcore-display-stx7109c3
modprobe stmfb display0=1280x720-32@50:12m:pal:yuv:yuv

echo "[rcS] Load audio firmware (LOAD 8)"
echo "   8"  > /dev/fpsmall
dtsdown=`cat /etc/enigma2/settings | grep systemoptions.dtsdownmix | cut -d "=" -f2`
if [ ! $dtsdown ] && [ -e /boot/audio_dts.elf ]; then
   /usr/bin/ustslave /dev/st231-1 /boot/audio_dts.elf > /dev/null
else
   /usr/bin/ustslave /dev/st231-1 /boot/audio.elf > /dev/null
fi

echo "[rcS] Load video firmware (LOAD 7)"
echo "   7"  > /dev/fpsmall
bin/ustslave /dev/st231-0 /boot/video.elf > /dev/null

echo "[rcS] Init embx (LOAD 6)"
echo "   6"  > /dev/fpsmall
modprobe embxshell
modprobe embxmailbox mailbox0=0xb9211000:136:set2 mailbox1=0xb9212000:0 ;
modprobe embxshm mailbox0=shm:0:7:0x60000000:0:256:256:0:1024:0xa4000000:0x10000000 ;
modprobe mme_host transport0=shm

echo "[rcS] Init AVS (LOAD 5)" 
echo "   5"  > /dev/fpsmall
modprobe avs type=stv6412

player=`cat /proc/stb/player/version | awk '{ print substr( $0, length($0) - 2, length($0) ) }'`
echo "[rcS] Init player $player (LOAD 4)"
echo "   4"  > /dev/fpsmall
modprobe mmelog
modprobe mpeg2hw mmeName=MPEG2_TRANSFORMER1
modprobe ksound
modprobe p2div64
modprobe stm_v4l2
modprobe pseudocard

modprobe cx24116

modprobe starci
modprobe pti waitMS=20 videoMem=4096
if [ $? -ne 0 ]; then
   modprobe pti
   echo "[rcS] Simple pti"
else
   echo "[rcS] Full feature pti"
fi
if [ $player=='player191' ]; then
   modprobe stm_monitor
   modprobe stmsysfs
   modprobe stmdvb
elif [ $player=='player179' ]; then
   modprobe stm_monitor
   modprobe stmsysfs
   modprobe stmdvb
else
   modprobe stmdvb
   modprobe stm_monitor
fi

echo "[rcS] Init eplayer2 (LOAD 3)"
echo "   3" > /dev/fpsmall
modprobe player2
modprobe sth264pp
modprobe stmalloc
modprobe platform
modprobe silencegen

modprobe bpamem
mknod /dev/bpamem c 153 0 

if [ -e /usr/share/bootlogo.mvi ]; then
   showiframe -p /usr/share/bootlogo.mvi &
fi
stfbcontrol a 0

# Workaround why is jfs always not mountable?
#fsck.ext3 /dev/sda3 -a
#fsck.jfs /dev/sda3 -a

echo "[rcS] Init remote control (LOAD 2)"
echo "   2" > /dev/fpsmall
modprobe boxtype
modprobe tfswitch
/usr/bin/evremote2 110 10 &

stfbcontrol a 255

wlan=`cat /etc/enigma2/settings | grep systemoptions.wlan | cut -d "=" -f2`
if [ ! $wlan ] || [ ! -e /lib/modules/2.6.32.71_stm24_0217/extra/$wlan ]; then
   echo "[rcS] No WLAN"
else
   modprobe $wlan
   echo "[rcS] $wlan WLAN"
#   echo "$wlan WLAN" > /dev/vfd
#   sleep 3
fi

if [ -e /etc/init.d/mountall ]; then
   /etc/init.d/mountall start
fi

if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/ntfs ]; then
   modprobe ntfs
fi

if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/cifs ]; then
   modprobe cifs
fi

echo "[rcS] Init FTP & network (LOAD 1)"
echo "   1" > /dev/fpsmall
/etc/init.d/vsftpd start
/usr/sbin/inetd

# Start automount
if [ -e /etc/init.d/automount ]; then
   python /etc/init.d/automount &
fi

# Do own extras
if [ -e /etc/ownscript.sh ]; then
   /etc/ownscript.sh &
fi

echo "[rcS] Start Enigma2"
echo "STRT" > /dev/fpsmall
echo "ENIGMA 2" > /dev/fplarge

until false
do
   echo "[rcS] Entering e2 main loop"
   if [ -e /usr/share/backdrop.mvi ]; then
      showiframe -p /usr/share/backdrop.mvi &
   fi
   /usr/bin/enigma2
   rtv=$?
   # update the GMT offset in case it changed while being turned on
   /usr/bin/tffpctl --setgmtoffset
   /usr/bin/tffpctl --cleardisplay
   echo "[rcS] e2 ended <- RTV: " $rtv
   case "$rtv" in
      0) echo "[rcS] E2 ERROR"
         echo "E2 ERROR" > /dev/fplarge
         #kill tuxtxt because it inhibits e2 from
         #restart after a crash. Of course this could
         #be done better ;-)
         /usr/bin/killall tuxtxt
         #rset
         showiframe -p /usr/share/bootlogo.mvi &
         echo "0" > /proc/progress;;
      1) echo "[rcS] SHUTDOWN"
         echo "SHUTDOWN" > /dev/fplarge
         if [ -e /usr/share/shutdown.mvi ]; then
             showiframe -p /usr/share/shutdown.mvi &
         fi
         init 0;;
      2) echo "[rcS] REBOOT"
         echo "REBOOT" > /dev/fplarge
         init 6;;
      3) echo "[rcS] E2 RESTART"
         echo " GUI" > /dev/fpsmall
         echo "RESTART" > /dev/fplarge
         if [ -e /usr/share/backdrop.mvi ]; then
            showiframe -p /usr/share/backdrop.mvi &
         else
            showiframe -p /usr/share/bootlogo.mvi &
         fi;;
      *) echo "[rcS] ERROR"
         echo "RESTART" > /dev/fplarge
         init 6;;
   esac
done
